<?php
//模型
class SubjectModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'subject';
	}
	
	function get_list($arr=array(),$page=1,$size=10){
		$result = array('list'=>array(),'count'=>0);
		
		$where = ' 1 ';
		
		if(isset($arr['name']) && !empty($arr['name'])){
			$where .= " AND s.name like '%{$arr['name']}%' ";
		}
		if(isset($arr['type']) && !empty($arr['type'])){
			$where .= " AND s.type = '{$arr['type']}' ";
		}
		
		if(isset($arr['parent_id']) && $arr['parent_id']!=-1){
			$where .= " AND s.parent_id = '{$arr['parent_id']}' ";
		}
		
		
		
		$sql  = "select s.*,IFNULL(st.name,'') as type_name from ".$this->db->dbprefix.$this->table." as s ".
			" LEFT JOIN ".$this->db->dbprefix."subject_type as st ON s.type = st.id".
			" where {$where} order by id desc limit ".($page-1)*$size.",{$size}";
		
		
		
		$query = $this->db->query($sql);
		$res = $query->result_array();
		$result['list'] = $res;
		
		
		$sql  = "select count(*) as num from ".$this->db->dbprefix.$this->table." as s ".
			" LEFT JOIN ".$this->db->dbprefix."subject_type as st ON s.type = st.id".
			" where {$where} ";
		
		$query = $this->db->query($sql);
		$row = $query->row_array();
		$result['count'] = $row['num'];
		
		return $result;
	}
	
	function get_info($id){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where id='{$id}' limit 1";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row;
	}
	
	function get_subject($name){
		$sql  = "select code from ".$this->db->dbprefix.$this->table." where name='{$name}' limit 1";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row;
	}

}

